#include <bits/stdc++.h>
using namespace std;
int t, n, m, a_[100005], a[100005], c[100005], X[5], d[20][131080], sum;
void dfs(int dep, int x, int y, int nn)
{
    if (dep == log2(nn) + 1)
    {
        sum += x;
        return;
    }
    if (x == nn)
        dfs(dep + 1, x / 2, x, nn);
    else if (y == nn)
    {
        if (d[dep][y / 2] == 1)
        {
            if (a[y] >= dep)
                dfs(dep + 1, y / 2 - 1, y, nn);
            else
                dfs(dep + 1, x / 2, x, nn);
        }
        else
        {
            if (a[x] >= dep)
                dfs(dep + 1, x / 2, x, nn);
            else
                dfs(dep + 1, y / 2 - 1, y, nn);
        }
    }
    else
    {
        if (d[dep][y / 2] == 1)
        {
            if (a[y] >= dep)
                a[y / 2] = a[y];
            else
                a[x / 2 + 1] = a[x];
            dfs(dep, x + 2, y + 2, nn);
        }
        else
        {
            if (a[x] >= dep)
                a[x / 2 + 1] = a[x];
            else
                a[y / 2] = a[y];
            dfs(dep, x + 2, y + 2, nn);
        }
    }
}
int main()
{
    freopen("arena.in", "r", stdin);
    freopen("arena.out", "w", stdout);
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
        cin >> a_[i];
    for (int i = 1; i <= m; i++)
        cin >> c[i];
    int cnt = ceil(log2(n));
    for (int i = 1; i <= cnt; i++)
        for (int j = 1; j <= pow(2, cnt - i); j++)
            cin >> d[i][j];
    cin >> t;
    while (t--)
    {
        cin >> X[0] >> X[1] >> X[2] >> X[3];
        for (int i = 1; i <= m; i++)
        {
            for (int j = 1; j <= n; j++)
                a[j] = a_[j] ^ X[j % 4];
            sum = 0;
            dfs(1, 1, 2, c[i]);
            for (int j = c[j] + 1; j <= pow(2, ceil(log2(c[j]))); j++)
                sum += j;
            cout << sum << endl;
        }
    }
    return 0;
}